package org.dairy.farms.dao.hibernate;

import org.dairy.farms.dao.ExpenseDAO;
import org.dairy.farms.dao.generic.AbstractDAOImpl;
import org.dairy.farms.model.*;
import org.hibernate.Criteria;
import org.hibernate.criterion.Conjunction;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: gduggira
 * Date: Jun 13, 2011
 * Time: 3:20:57 PM
 */
@Repository(value = "expenseDao")
public class ExpenseDAOImpl extends AbstractDAOImpl<Expense, Long> implements ExpenseDAO {

    @Override
    public List<Expense> findByLivestockId(Long livestockId, int page, int start, int limit) {
        Criteria criteria = basePersistence.getSession().createCriteria(LivestockExpense.class);
        criteria.createCriteria("expenseType").add(Restrictions.in("name", new String[]{ExpenseType.EXPENSE_TYPE_OF_LIVESTOCK,
                ExpenseType.LIVESTOCK_CHECKUP_EXPENSE,
                ExpenseType.LIVESTOCK_FEED_EXPENSE,
                ExpenseType.LIVESTOCK_MISC_EXPENSE}));
        criteria.createCriteria("checkup").createCriteria("livestock").add(Restrictions.eq("id", livestockId));
        criteria.createCriteria("feed").createCriteria("livestock").add(Restrictions.eq("id", livestockId));
        criteria.createCriteria("livestock").add(Restrictions.eq("id", livestockId)).setFirstResult(start).setMaxResults(limit);
        return criteria.list();
    }

    @Override
    public List<Expense> findByLivestockId(Long livestockId) {
        Criteria criteria = basePersistence.getSession().createCriteria(Expense.class);
        criteria.createCriteria("expenseType", "et");
        criteria.add(Restrictions.eq("et.expenseTypeOf", ExpenseType.EXPENSE_TYPE_OF_LIVESTOCK));
        return criteria.list();
    }
}

/*Criterion liveStockExpense    = Restrictions.eq("class", LivestockExpense.class);
        Criterion feedExpense = Restrictions.eq("class", FeedExpense.class);
        Criterion checkupExpense = Restrictions.eq("class", CheckupExpense.class);

        Disjunction disjunction = Restrictions.disjunction();
        disjunction.add(liveStockExpense);
        disjunction.add(feedExpense);
        disjunction.add(checkupExpense);
*/
        /*Conjunction livestockConjunction = Restrictions.conjunction();
        livestockConjunction.add(liveStockExpense);
        livestockConjunction.add(Restrictions.eq("livestock.id", livestockId));

        Conjunction feedExpenseConjunction = Restrictions.conjunction();
        feedExpenseConjunction.add(feedExpense);
        feedExpenseConjunction.add(Restrictions.eq("feed.livestock.id", livestockId));

        Conjunction checkupExpenseConjunction = Restrictions.conjunction();
        checkupExpenseConjunction.add(checkupExpense);
        checkupExpenseConjunction.add(Restrictions.eq("checkup.livestock.id", livestockId));
*//*
        disjunction.add(checkupExpenseConjunction);
        disjunction.add(feedExpenseConjunction);
        disjunction.add(livestockConjunction);*/

        //criteria.add(disjunction);
